package com.campus.mapper.Impl;

import com.campus.domain.Dto.StudentBatchAddDto;
import com.campus.domain.Dto.StudentCreateDto;
import com.campus.domain.Dto.UserRegisterDto;
import com.campus.domain.Vo.StudentExamineBatchVo;
import com.campus.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class StudentMapperImpl implements StudentMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer addStudent(StudentCreateDto studentCreateDto,Long loginId) {
        String sql = "Insert into t_manager_student (stu_name, from_place, parent_phone, current_grade, is_first, login_id, comment, status) values (?,?,?,?,?,?,?,?)";

        Object[] args = new Object[]{
                studentCreateDto.getStuName(),
                studentCreateDto.getFromPlace(),
                studentCreateDto.getParentPhone(),
                studentCreateDto.getCurrentGrade(),
                studentCreateDto.getIsFirst(),
                loginId,
                studentCreateDto.getComment(),
                "0"
        };

        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer examineStudent(Long id) {
        String sql = "update t_manager_student set status = '1' where id = ?";

        Object[] args = new Object[]{
                id
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public List<StudentExamineBatchVo> selectForExamine() {
        String sql = "select * from t_manager_student where status = '0'";
        return jdbcTemplate.query(sql,rs -> {
            List<StudentExamineBatchVo> list = new ArrayList<>();
            while (rs.next()){
                StudentExamineBatchVo student = new StudentExamineBatchVo();
                student.setId(rs.getLong("id"));
                student.setStuName(rs.getString("stu_name"));
                student.setFromPlace(rs.getString("from_place"));
                student.setParentPhone(rs.getString("parent_phone"));
                student.setCurrentGrade(rs.getString("current_grade"));
                student.setIsFirst(rs.getString("is_first"));
                student.setComment(rs.getString("comment"));
                student.setStatus(rs.getString("status"));
                list.add(student);
            }
            return list;
         });
    }

}
